from bs4 import BeautifulSoup as bs
import requests,xlwt
def saveExcel(title, data):
    wbt = xlwt.Workbook()
    sheet = wbt.add_sheet('排名表')
    for i in range(len(title)):
        sheet.write(0, i, title[i])
    for m in range(len(data)):
        for n in range(len(data[m])):
            sheet.write(m+1, n, data[m][n])
    wbt.save('TOP600school.xls')
url_string = r'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html'
header= {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64)',
    'Host':'www.zuihaodaxue.cn'
}
req = requests.get(url_string,headers=header)
soup=bs(req.content,'html.parser')
title=[]
for item in soup.find_all('th'):
    if item.string != None:
        title.append(item.string)
for item in soup.find_all(id='select-indicator-type'):
    for i in item.find_all('option'):
        title.append(i.string)
data=[]
for item in soup.find_all('tr',class_='alt'):
    list_one = []
    for i in item.find_all('td'):
        list_one.append(i.string)
    data.append(list_one)
saveExcel(title, data)

